<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
error_log("访问游戏ID: " . $_GET['yxid']);

session_start();
include('../config/conn.php');

// 检查是否登录
if(!isset($_SESSION['username']) || empty($_SESSION['username'])) {
    header('Location: /index.php');
    exit();
}

// 获取游戏ID
$yxid = isset($_GET['yxid']) ? intval($_GET['yxid']) : 0;
if($yxid <= 0) {
    die('无效的游戏ID');
}

// 获取用户ID
$uid = $_SESSION['uid'];

// 查询用户权限
$usql = "SELECT * FROM user WHERE id = ?";
$stmt = $conn->prepare($usql);
$stmt->bind_param("i", $uid);
$stmt->execute();
$uresult = $stmt->get_result();

if ($uresult->num_rows > 0) {
    $urow = $uresult->fetch_assoc();
    
    // 查询游戏信息
    $xysql = "SELECT * FROM game WHERE id = ?";
    $stmt = $conn->prepare($xysql);
    $stmt->bind_param("i", $yxid);
    $stmt->execute();
    $yxresult = $stmt->get_result();
    
    if($yxresult->num_rows > 0) {
        $game = $yxresult->fetch_assoc();
        
        // 检查用户权限是否满足游戏要求
        if($urow['vip'] >= $game['vip']) {
            // 有权限，重定向到游戏URL
            header('Location: ' . $game['url']);
            exit();
        } else {
            // 没有权限，提示升级
            echo "<script>alert('您的会员等级不足，请升级会员！');window.location.href='/profile.php';</script>";
        }
    } else {
        die('游戏不存在');
    }
} else {
    die('用户信息获取失败');
}
?>